<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <br/>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="#" min-width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" min-width="360"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="80"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column prop="goods_state" label="商品状态" width="80"></el-table-column>
        <el-table-column prop="add_time" label="添加时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goods_id)"></el-button> <!-- 修改按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoodsById(scope.row.goods_id)"></el-button> <!-- 删除按钮 -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 layout指定分页器的布局结构 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10, 20]" :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>

    <!-- 修改商品的对话框 -->
    <el-dialog title="修改商品" :visible.sync="editGoodDialogVisible" width="50%" @close="editGoodDialogClosed">
      <!-- 内容主体区 -->
      <el-form :model="editGoodForm" :rules="editGoodFormRules" ref="editGoodFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editGoodForm.goods_name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editGoodForm.goods_price" placeholder="请输入商品价格(元)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editGoodForm.goods_number" placeholder="请输入商品数量" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editGoodForm.goods_weight" placeholder="请输入商品重量(kg)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="商品介绍">
          <div class="info" v-html="editGoodForm.goods_introduce"></div>
        </el-form-item>
        <el-form-item label="商品图片">
          <div class="img-list">
            <div class="good-img" v-for="item in editGoodForm.pics" :key="item.pics_id">
              <img :src="item.pics_mid_url" alt="">
            </div>
          </div>
        </el-form-item>
        <el-form-item label="商品参数">
          <div class="info">
            <div v-for="(item, index) in editGoodForm.attrs" :key="index">
              <div style="font-weight: bolder; font-size: 14px;">{{item.attr_name}}</div>
              <div>{{item.attr_vals}}</div>
            </div>
          </div>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="editGoodDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGood">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Goods',
  data() {
    return {
      // 获取商品列表的参数对象
      queryInfo: {
        query: '',
        pagenum: 1, // 当前的页数
        pagesize: 10 // 每页显示多少条数据
      },

      goodsList: [], // 商品列表
      total: 0, // 总数据条数

      editGoodDialogVisible: false, // 控制修改商品的对话框的显示与隐藏
      editGoodForm: {}, // 修改商品的表单数据
      editGoodFormRules: { // 添加商品表单的验证规则对象
        goods_name: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '商品价格不能为空', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '商品数量不能为空', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '商品重量不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 根据分页获取对应的获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.$message.success('获取商品列表成功')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },

    // 监听 page-size 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },

    // 点击编辑按钮, 显示修改对话框
    async showEditDialog(id) {
      const { data: res } = await this.$http.get(`goods/${id}`)
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('查询分类信息失败')
      this.editGoodForm = res.data
      this.editGoodDialogVisible = true
    },
    // 监听修改商品对话框的关闭事件
    editGoodDialogClosed() {
      this.$refs.editGoodFormRef.resetFields()
    },
    // 点击确定按钮,提交修改商品
    editGood() {
      // console.log(this.editGoodForm)
      this.$refs.editGoodFormRef.validate(async valid => {
        if (!valid) return
        // console.log(this.editGoodForm)
        // 验证通过,发起修改商品信息的网络请求
        const { data: res } = await this.$http.put('goods/' + this.editGoodForm.goods_id, this.editGoodForm)
        // console.log(res)
        if (res.meta.status !== 200) {
          this.$message.error('更新商品信息失败')
          return
        }
        this.$message.success('更新商品信息成功')
        this.editGoodDialogVisible = false
        this.getGoodsList()
      })
    },

    // 点击删除按钮, 根据id删除对应的商品信息
    async removeGoodsById(id) {
      // 弹框询问用户是否删除该商品数据
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 如果用户确认删除,则返回值为字符串confirm ; 如果用户取消删除,则返回值为字符串cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }
      this.$message.success('删除商品成功!')
      this.getGoodsList()
    },

    // 点击添加商品按钮跳转到添加商品页面
    goAddPage(id) {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
  .info {
    width: 100%;
    height: 100px;
    overflow: auto;
    padding: 5px 15px;
    background-color: #FFF;
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    box-sizing: border-box;
  }
  .img-list {
    width: 100%;
    min-height: 70px;
    padding: 5px 15px;
    background-color: #FFF;
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    .good-img {
      width: 80px;
      height: 60px;
      border-radius: 10px;
      img {
        width: 80px;
        height: 60px;
      }
    }
  }
</style>
